/*******************************************************************************
 * Copyright 2011 Towee.net
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *******************************************************************************/
package net.towee.client.com.repo.presenter;

import java.util.HashMap;
import java.util.Map;

import net.towee.client.com.app.AppController.Component;
import net.towee.client.com.repo.RepositoryExplorer;
import net.towee.client.com.repo.RepositoryShower;
import net.towee.client.event.core.UserEnteredToGroupEvent;
import net.towee.client.event.core.UserEnteredToGroupHandler;
import net.towee.client.event.core.UserLeaveFromGroupEvent;
import net.towee.client.event.core.UserLeaveFromGroupHandler;
import net.towee.client.history.HistoryEvent;
import net.towee.client.history.HistoryHandler;
import net.towee.client.history.HistoryManager;
import net.towee.client.managers.AccountHelper;
import net.towee.client.managers.GroupsManager;
import net.towee.client.managers.NodesManager;
import net.towee.model.account.AccountGroup;
import net.towee.model.group.GroupIdentity;
import net.towee.model.repo.NodeData;
import net.towee.model.repo.impl.NodeDataImpl;

import com.google.gwt.event.logical.shared.HasSelectionHandlers;
import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.IsWidget;
import com.google.inject.Inject;

/**
 * 
 * @author viiv.c58@gmail.com (Artem Artem'ev)
 */
public class RepositoryExplorerPresenter implements RepositoryExplorer, SelectionHandler<GroupIdentity>, HistoryHandler {
	public interface Display extends IsWidget {
		/**
		 * Show group
		 * @param identity
		 * @return
		 */
		void addGroup(GroupIdentity identity);

		/**
		 * Remove group from display
		 * @param identity
		 */
		void removeGroup(GroupIdentity identity);
		
		/**
		 * Select given group
		 * @param identity
		 */
		void selectGroup(GroupIdentity identity);
		
		/**
		 * Get group selector 
		 */
		HasSelectionHandlers<GroupIdentity> getSelector();
	}
	
	private final Display display;
	private final GroupsManager groups;
	private final AccountHelper account;
	private final NodesManager nodes;
	private final RepositoryShower shower;
	private final HandlerManager eventBus;
	private final HistoryManager history;
	
	private final Map<String, Boolean> loadedNodes;
	
	@Inject
	public RepositoryExplorerPresenter(Display display, HandlerManager eventBus,
			GroupsManager groups, AccountHelper account, NodesManager nodes, RepositoryShower shower,
			HistoryManager history){
		this.display = display;
		this.nodes = nodes;
		this.shower = shower;
		this.eventBus = eventBus;
		this.history = history;
		
		this.groups = groups;
		this.account = account;
		this.loadedNodes = new HashMap<String, Boolean>();
		
		// Add history handler
		this.history.addHandler(Component.REPO, this);
		
		// Load groups
		loadGroups();
		
		// Bind events
		bindEvents();
		display.getSelector().addSelectionHandler(this);
	}

	private void bindEvents() {
		eventBus.addHandler(UserEnteredToGroupEvent.TYPE, new UserEnteredToGroupHandler(){
			@Override
			public void onUserEnteredToGroup(AccountGroup acGroup) {
				display.addGroup(acGroup.getGroupIdentity());
			}
		});
		
		eventBus.addHandler(UserLeaveFromGroupEvent.TYPE, new UserLeaveFromGroupHandler(){
			@Override
			public void onUserLeaveGroup(GroupIdentity identity) {
				display.removeGroup(identity);
			}
		});
	}

	/**
	 * Init method. Add root nodes named as groups, where user
	 * entered.
	 */
	private void loadGroups() {
		for(AccountGroup g : account.getAccount().getGroups().getGroupsList()) {
			display.addGroup( g.getGroupIdentity() );
		}
	}

	@Override
	public void onSelection(SelectionEvent<GroupIdentity> event) {
		// Create root node
		GroupIdentity group = event.getSelectedItem();
		NodeData node = new NodeDataImpl(null, group.getId(), group.getPrintName());
		
		// Show node
		shower.showTreeNode(node);
	}
	
	@Override
	public void go(HasWidgets container) {
		container.clear();
		container.add( display.asWidget() );
	}

	@Override
	public void onHistoryEvent(HistoryEvent event) {
		if( event.getParameters().containsKey("group") ) {
			String groupId = event.getParameters().get("group");
			
			if(account.getAccountGroup(groupId) != null) {
				display.selectGroup( account.getAccountGroup(groupId).getGroupIdentity() );
			}
		}
	}

	@Override
	public void selectGroup(GroupIdentity group) {
		display.selectGroup(group);
	}
}